//
//  ICUserInfoCell.m
//  ICome
//
//  Created by zhangrongwu on 16/9/12.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICUserInfoCell.h"
#import "ICHeaderFile.h"

@interface ICUserInfoCell(){
    UIMenuItem * _copyMenuItem;
}
@end

@implementation ICUserInfoCell
- (id)initWithStyle:(UITableViewCellStyle)cell reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:cell reuseIdentifier:reuseIdentifier];
    if (self) {
        [self layoutSubview];
        
        UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
        [self addGestureRecognizer:longPressGesture];

    }
    return self;
}

- (void)copyComment:(UIMenuController *)menu {
    UIPasteboard *pasteboard  = [UIPasteboard generalPasteboard];
    pasteboard.string = self.detailLabel.text;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(copyComment:)) {
        return YES;
    }
    return  [super canPerformAction:action withSender:sender];
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}
- (void)longPress:(UILongPressGestureRecognizer *)longRecognizer
{
    if (longRecognizer.state == UIGestureRecognizerStateBegan) {
        [self becomeFirstResponder];
        [[UIMenuController sharedMenuController] update];
        if (!_copyMenuItem) {
            _copyMenuItem   = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyComment:)];
        }
        [[UIMenuController sharedMenuController] setMenuItems:@[_copyMenuItem]];
        [[UIMenuController sharedMenuController] setTargetRect:self.frame inView:self.superview];
        [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
    }
}

- (void)layoutSubview {
    _margin = 15;
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.mas_left).offset(self.margin);
    }];
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.titleLabel.mas_right).offset(15);
        make.right.equalTo(self.mas_right).offset(-self.margin-15);
//        make.width.mas_equalTo(App_Frame_Width - 80);
    }];
    
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-self.margin);
    }];
}

-(void)setModel:(ICContactItem *)model {
    _model = model;
    self.titleLabel.text = model.title;
    self.detailLabel.text = model.detailTitle;
    [self.rightButton setBackgroundImage:[UIImage imageNamedFromiComeSDKBundle:model.icon] forState:UIControlStateNormal];
    
    CGSize size =[model.title sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
    
    
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.mas_left).offset(self.margin);
        make.right.equalTo(self.mas_left).offset(self.margin+size.width+10);
    }];
    
    [self.detailLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.titleLabel.mas_right).offset(15);
        make.right.equalTo(self.mas_right).offset(-self.margin-15);
    }];

}

- (UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48.0f, 48.0f)];
        CAShapeLayer *layer = [CAShapeLayer layer];
        UIBezierPath *apath = [UIBezierPath bezierPathWithOvalInRect:_headImageView.bounds];
        layer.path = apath.CGPath;
        _headImageView.layer.mask = layer;
        _headImageView.userInteractionEnabled = YES;
        [self addSubview:_headImageView];
    }
    return _headImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:17];
        _titleLabel.preferredMaxLayoutWidth = 200 / 375 * App_Frame_Width;
        [_titleLabel sizeToFit];
        _titleLabel.textColor = ICTEXTCOLOR_T1;
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

-(UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = [UIFont systemFontOfSize:16];
        _detailLabel.textColor = ICTEXTCOLOR_T1;
        [self addSubview:_detailLabel];
        _detailLabel.textAlignment = NSTextAlignmentLeft;
        _detailLabel.numberOfLines = 2;
    }
    return _detailLabel;
}

-(UITextField *)edtDetailField {
    if (!_edtDetailField) {
        _edtDetailField.font = [UIFont systemFontOfSize:16];
        _edtDetailField = [[UITextField alloc] init];
        _edtDetailField.textColor = ICTEXTCOLOR_T1;
        _edtDetailField.textAlignment = NSTextAlignmentRight;
        _edtDetailField.hidden = YES;
        [self addSubview:_edtDetailField];
    }
    return _edtDetailField;
}

-(UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [[UIButton alloc] initWithTitle:@"" textColor:nil backImg:@"" font:12];
        [self addSubview:_rightButton];
    }
    return _rightButton;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.bottomLine mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.right.equalTo(self.mas_right);
        make.height.mas_equalTo(0.5);
        make.bottom.equalTo(self.contentView.mas_bottom);
    }];
}






@end
